package com.ubing.boot.notesforjava.test.juc;

import java.io.Serializable;
import java.util.concurrent.TimeUnit;

/**
 * @ClassName : A
 * @Description : A
 * @Author : Xxxxx
 * @Date: 2023-03-08 15:04
 */
public class A implements Serializable {
    public static void main(String[] args) throws InterruptedException {
//        Scanner scanner = new Scanner(System.in);
//        while (true){
//            int a = scanner.nextInt();
//            System.out.println(A.checkMustCopy(a));
//        }
//    }
//
//    public static int checkMustCopy(int num){
//            int checkNum=num;
//            while (true){
//                if(((checkNum%10)^(checkNum/10%10))==0){
//                    checkNum=++num;
//                }else {
//                    checkNum/=10;
//                    if(checkNum/10==0) return num;
//                }
//            }
//    }
        lock lock = new lock();
        Thread thread = new Thread(() -> {
            try {
                lock.incCount1();
            } catch (Exception e) {
                // 异常会释放锁
                System.out.println("catch====================>");
            }
        });
        Thread thread1 = new Thread(() -> {
            try {
                lock.getCount1();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        });
        thread.start();
        thread1.start();
        thread.join();
        thread1.join();
    }
}
class lock{

    private Integer conunt=100;
    private Object o = new Object();//可当锁
    private Object o1 = new Object();
    public synchronized void incCount(){
        for (int i = 0; i <110 ; i++) {
            System.out.println("inc==>"+conunt);
            if(conunt<0){
                System.out.println("end");
                break;
            }
            conunt--;
        }
    }

    void incCount2(){
        synchronized (this){ // 等值于    public synchronized void incCount()
            for (int i = 0; i <110 ; i++) {
                System.out.println("inc==>"+conunt);
                if(conunt<0){
                    System.out.println("end");
                    break;
                }
                conunt--;
            }
        }
    }
    public void getCount2(){
        for (int i = 0; i <110 ; i++) {
            System.out.println("get==>"+conunt);
        }
    }


    void incCount1() throws Exception {
        synchronized (o1){
            for (int i = 0; i <110 ; i++) {
                System.out.println("inc==>"+conunt);
                if(conunt<0){
                    System.out.println("end");
                    break;
                }
                conunt--;
            }
            TimeUnit.SECONDS.sleep(2);
//            int a=1/0;//  异常会导致锁讲被废弃
            synchronized (o){
                for (int i = 0; i <110 ; i++) {
                    System.out.println("inc==>"+conunt);
                    if(conunt<0){
                        System.out.println("end");
                        break;
                    }
                    conunt--;
                }
            }
        }

    }

    void getCount1() throws InterruptedException {
        synchronized (o){
            for (int i = 0; i <110 ; i++) {
                System.out.println("get==>"+conunt);
            }
            TimeUnit.SECONDS.sleep(2);
            synchronized (o1){
                for (int i = 0; i <110 ; i++) {
                    System.out.println("get==>"+conunt);
                }
            }
        }

    }


    public synchronized void getCount(){
        for (int i = 0; i <110 ; i++) {
            System.out.println("get==>"+conunt);
        }
    }
}